<?
require_once("../index.php");

$db = Zend_Db::factory($config->db);
 
$resources = new Zend_Config_Ini('resources.ini');
 
try {
    echo "\n>>> Delete acl_resources table\n";
    $db->query('DROP TABLE IF EXISTS `acl_resources`');
    $db->query('DROP TABLE IF EXISTS `acl_permissions`');
} catch (Exception $e) {
    echo $e->getMessage();
}
 
try {
    echo "\n>>> Re-creating acl_resouces table\n";
    $db->query('CREATE TABLE `acl_resources` (
                `uid` INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
                `resource` VARCHAR(64) NOT NULL UNIQUE);
               ');
 
    echo "\n>>> Re-creating acl_resouces table\n";
    $db->query('CREATE TABLE `acl_permissions` (
                `role_id` INTEGER(1) NOT NULL,
                `resource_uid` INTEGER(4) NOT NULL,
                `permission` VARCHAR(64) NOT NULL);
               ');
 
    foreach ($resources as $key => $value) {
 
        $db->query('INSERT INTO `acl_resources` (resource) VALUES ("' . $key . '")');
        $lastInsertId= $db->lastInsertId();
         
        foreach ($value as $key2 => $value2) {
 
 
            $sql = $db->query('SELECT `role_id` FROM `acl_roles` WHERE `role_name` = "' . $value2 . '"');
            $role = $sql->fetchObject();
            if ($role) {
                $db->query('INSERT INTO `acl_permissions` (role_id, resource_uid, permission) VALUES ("'.$role->role_id.'", ' . $lastInsertId . ', "' . $key2 . '")');
            } else {
                echo "ERROR! resource " . $value2 . " not found!";
            }
              
        }
    }
 
    echo "\n>>> ACL Resources rebuild complete\n\n";
} catch (Exception $e) {
    echo $e->getMessage();
}
